namespace.reg("menuManage.fun");
menuManage.fun = (function () {
    var initGrid = function (url) {
        var initTableData = function (currentPage, pageSize, parame) {
            if (baseJS.isEmpty(currentPage)) {
                currentPage = 1;
            }
            if (baseJS.isEmpty(pageSize)) {
                pageSize = 10;
            }
            var tableInitParam = {};
            var datas = {currentPage: currentPage, pageSize: pageSize};
            if (!baseJS.isEmpty(parame)) {
                $.extend(datas, parame);
            }
            baseJS.ajaxRequest({
                url: "/menu/list",
                data: datas,
                success: function successCallback(reqData) {
                    tableInitParam = {
                        tableData: reqData.data,
                        total: reqData.number,
                        pageSize: 10,
                        currentPage: 1,
                    };
                },
                error: function errorCallback() {
                    baseJS.layerMsg("ajaxRequest请求出错");
                }
            });
            return tableInitParam;
        }

        var deleteTableData = function (id, callback) {
            $.ajax({
                url: "/backstage/deleteUser",
                dataType: "json",
                async: false,
                data: {id: id},
                type: "post",
                success: function (reqData) {
                    if (callback != null && typeof(callback) == "function") {
                        callback(reqData);
                    } else {
                        if (reqData.status) {
                            baseJS.layerMsg(reqData.msg);
                        }
                    }
                },
                error: function () {
                    baseJS.layerMsg("删除出错");
                }
            });
        };
        var app = new Vue({
            data: function () {
                var reqIniData = initTableData(1, 10);
                return {
                    item: null,
                    type: "add",
                    total: reqIniData.total,
                    pageSize: reqIniData.pageSize,
                    currentPage: reqIniData.currentPage,
                    selectRow: [],
                    layerIndex: '',
                    loadedData: function (currentPage, pageSize, parame) {
                        var pageData = initTableData(currentPage, pageSize, parame);
                        this.pageSize = pageData.pageSize;
                        this.total = pageData.total;
                        this.tableData = pageData.tableData;
                    },
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },
                        {
                            title: '资源ID',
                            key: 'id'
                        },
                        {
                            title: '资源名称',
                            key: 'name'
                        },
                        {
                            title: '父资源ID',
                            key: 'parentId'
                        },
                        {
                            title: '资源链接',
                            key: 'resUrl'
                        },
                        {
                            title: '资源类型',
                            key: 'type'
                        },
                        {
                            title: '操作',
                            key: 'action',
                            width: 150,
                            align: 'center',
                            render: function (h, params) {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: function () {
                                                app.editFun(params.index, params.row)
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'error',
                                            size: 'small'
                                        },
                                        on: {
                                            click: function () {
                                                app.delete(params.index)
                                            }
                                        }
                                    }, '删除')
                                ]);
                            }
                        }
                    ],
                    tableData: reqIniData.tableData
                }
            },
            methods: {
                handleRowChange(selection) {
                    this.selectRow = selection;
                },
                onPageSizeChange: function (value) {
                    this.loadedData(1, value);
                },
                delete: function (index) {
                    baseJS.layerOpenAle("确定要删除吗？", "温馨提示", function () {
                        deleteTableData(vueGridApp.tableData[index].id, function callback(reqData) {
                            if (reqData.status) {
                                vueGridApp.tableData.splice(index, 1);
                                if (vueGridApp.tableData.length == 0) {
                                    vueGridApp.loadedData(vueGridApp.currentPage - 1, vueGridApp.pageSize);
                                }
                                baseJS.layerMsg(reqData.msg);
                            }
                        });
                    });
                },
                deleteBatch: function (ev) {
                    var vueThis = this;
                    const selectRow = this.selectRow;
                    if (selectRow.length == 0) {
                        baseJS.layerMsg("请选择要删除的数据");
                        return true;
                    } else {
                        baseJS.layerOpenAle("确定要删除吗？", "温馨提示", function () {
                            var itemIds = [];
                            for (var index in selectRow) {
                                itemIds.push(selectRow[index].id);
                            }
                            baseJS.ajaxRequest({
                                url: "/backstage/userManage/deleteBatch",
                                data: {itemIds: itemIds.join(",")},
                                success: function successCallback(data) {
                                    if (!baseJS.isEmpty(data) && data.status == 1) {
                                        baseJS.layerMsg(data.msg, function callback() {
                                            vueThis.loadedData(vueThis.currentPage, vueThis.pageSize);
                                        });
                                    } else {
                                        baseJS.layerMsg("删除失败！");
                                    }
                                },
                                error: function errorCallback() {
                                    baseJS.layerMsg("删除失败！");
                                }
                            });
                        });
                    }
                },
                addButt: function () {
                    var vueThis = this;
                    this.type = "add";
                    baseJS.baseLayerOpen({
                        url: "/backstage/menuManage/addMenu"
                        , title: "新增用户"
                        , type: 2
                        , width: 650
                        , height: 300
                        , maxmin: false
                        , successCallbackLayerOpenFun: function (val, indexs) {
                            vueThis.layerIndex = indexs;
                        }
                    });
                },
                editFun: function (index, row) {
                    var vueThis = this;
                    this.item = row;
                    this.type = "edit";
                    baseJS.baseLayerOpen({
                        url: "/backstage/menuManage/addMenu"
                        , title: "编辑资源"
                        , type: 2
                        , width: 650
                        , height: 300
                        , maxmin: false
                        , successCallbackLayerOpenFun: function (val, indexs) {
                            vueThis.layerIndex = indexs;
                        }
                    });
                },
                pageNext: function (currentPage) {
                    this.loadedData(currentPage, this.pageSize);
                }
            }
        });
        vueGridApp = app;
        app.$mount('#app')
    }
    var initForm = function (item) {
        var formApp = new Vue({
            data: function () {
                const menuTypeList = [
                    {
                        label: '菜单',
                        value: '1'
                    },
                    {
                        label: '按钮',
                        value: '2'
                    },
                    {
                        label: '目录',
                        value: '3'
                    }
                ];
                const formValidateParamet = {
                    loading: false,
                    disabledReset: false,
                    menuTypeList: menuTypeList,
                    type: '1',
                    name: '',
                    resUrl: '',
                    icon: '',
                    parentId: 0,
                    sort: 0
                };
                if (item != null) {
                    $.extend(formValidateParamet, item);
                    formValidateParamet.type = formValidateParamet.type.toString();
                    parent.vueGridApp.item = null;
                }

                return {
                    formValidate: formValidateParamet,
                    ruleInline: {
                        name: [
                            {required: true, message: '请输入资源名称...', trigger: 'blur'}
                        ]
                    }
                }
            },
            methods: {
                onSubmit: function (name) {
                    this.$refs[name].validate((valid) => {
                        if (valid) {
                            var datas = this.formValidate;
                            const parameter = {
                                type: datas.type,
                                name: datas.name,
                                resUrl: datas.resUrl,
                                icon: datas.icon,
                                parentId: datas.parentId,
                                sort: datas.sort
                            };
                            var url = "/menu/addMenu";
                            var msg = "新增";
                            if (!baseJS.isEmpty(item)) {
                                url = "/menu/updateMenu";
                                parameter.id = item.id;
                                msg = "修改";
                            }
                            var req = baseJS.ajaxRequestSsyncFalse(url, parameter);
                            if (!baseJS.isEmpty(req) && req.status == 1) {
                                this.formValidate.loading = false;
                                this.formValidate.disabledReset = false;
                                parent.vueGridApp.loadedData(parent.vueGridApp.currentPage, parent.vueGridApp.pageSize);
                                baseJS.layerMsg(msg + "成功！", function callback() {
                                    parent.layer.close(parent.vueGridApp.layerIndex);
                                });
                            } else {
                                baseJS.layerMsg(msg + "失败！");
                            }
                        }
                    })

                },
                handleReset: function (name) {
                    this.$refs[name].resetFields();
                    const roleSelect = item.roleList;
                    this.formValidate.roleSelect = [];
                    for (var index in roleSelect) {
                        this.formValidate.roleSelect.push(roleSelect[index].roleId);
                    }
                }
            }
        });
        formApp.$mount('#formApp');
    }

    var getRoleList = function () {
        var reqData = null;
        baseJS.ajaxRequest({
            url: "/role/getRoleList",
            success: function success(data) {
                reqData = data.data;
            },
            error: function error() {
                baseJS.layerMsg("获取角色失败");
            }
        });
        return reqData;
    }

    return {
        initGrid: initGrid,
        initForm: initForm
    }
})();

